package solved;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;

/*
ID:		liluyan1
LANG:	JAVA
TASK:	spin
*/
public class spin {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("spin.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"spin.out")));
		new spin().solve(f, out);
		out.close();
		System.exit(0);
	}
	
	private void solve(BufferedReader f, PrintWriter out) throws IOException {
		int N = 5;
		int[] speed = new int[N];
		ArrayList<Wedge>[] wedges = new ArrayList[N];
		for (int i=0; i<N; i++) {
			String[] t = f.readLine().split(" ");
			speed[i] = Integer.parseInt(t[0]);
			int len = Integer.parseInt(t[1]);
			
			wedges[i] = new ArrayList<Wedge>();
			for (int j=0; j<len; j++) {
				int start = Integer.parseInt(t[2+2*j]);
				int degree = Integer.parseInt(t[3+2*j]);
				wedges[i].add(new Wedge(start, degree));
			}
		}
		
		HashSet<String> states = new HashSet<String>();
		for (int i=0; ; i++) {
			String s = toString(wedges);
			if (states.contains(s)) break;
			states.add(s);
			
			if (satisfy(wedges)) {
				out.println(i);
				return;
			}
			
			for (int j=0; j<N; j++) {
				for (Wedge w : wedges[j]) {
					w.start = (w.start + speed[j]) % 360;
				}
			}
		}
		
		out.println("none");
	}
	
	private boolean satisfy(ArrayList<Wedge>[] wedges) {
		for (int degree=0; degree<360; degree++) {
			boolean all = true;
			for (int i=0; i<5; i++) {
				boolean through = false;
				for (Wedge w : wedges[i]) {
					if (through(degree, w)) {
						through = true;
						break;
					}
				}
				if (!through) {
					all = false;
					break;
				}
			}
			if (all) return true;
		}
		return false;
	}
	
	private boolean through(int degree, Wedge w) {
		int end = w.start + w.degree;
		if (end <= 360) return degree >= w.start && degree <= end;
		return (degree >= w.start && degree < 360) || (degree >= 0 && degree <= end - 360);
	}
	
	private String toString(ArrayList<Wedge>[] wedges) {
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<5; i++) sb.append(" " + wedges[i].get(0).start);
		return sb.substring(1);
	}
	
	private class Wedge {
		int start;
		int degree;
		public Wedge(int start, int degree) {
			this.start = start;
			this.degree = degree;
		}
	}
}
